package com.sino.operation.service.business;

import com.sino.operation.Exception.exceptionRoot.SinoException;
import com.sino.operation.commons.response.CenterResponseEnum;
import com.sino.operation.config.Spring.SpringContextUtils;
import com.sino.operation.dto.Dto;
import com.sino.operation.dto.out.NetHandleEntity;
import com.sino.operation.peripheral.esc.request.NetHandle;
import com.sino.operation.service.business.inter.ITransactionHandler;
import com.sino.operation.service.impl.BizTranslistServiceImpl;
import com.sino.operation.util.PaginateUtil;
import com.sino.operation.util.ReflectExecuteUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Introspector;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: NetHandleListService
 * @Description: 网点查询已处理任务队列
 * @Author dengls
 * @date 2021/10/20 11:05
 */
@Slf4j
@Service
public class NetHandleListService {

    @Autowired
    private BizTranslistServiceImpl transListService;

    private static final List<String> tranCodeList = new ArrayList<String>() {{
        add("710301");
        add("710302");
        add("710304");
        add("710305");
        add("710306");
        add("710307");
        add("710308");
    }};

    public Dto execute(NetHandle netHandle) {
        log.info("查询网点已处理任务队列开始---NetHandleListService.execute");
        Dto dto = new Dto();
        try {
            /*解析报文，获取需要的数据*/
            String transCode = netHandle.getTransCode();
            if (StringUtils.isNotBlank(transCode)) {
                if (!tranCodeList.contains(transCode)) {
                    log.error("网点已处理任务队列报文错误，任务类型不存在");
                    throw new SinoException(CenterResponseEnum.PARAM_ERROR);
                }
                String serviceName = "Exec_" + transCode;
                try {
                    dto = ReflectExecuteUtil.getTaskList(serviceName, netHandle);
                } catch (Exception e) {
                    log.error("网点已处理任务队列出错", e);
                    throw new SinoException(CenterResponseEnum.PARAM_ERROR);
                }
            } else {
                dto = getTaskList(netHandle);
            }
        } catch (Exception e) {
            log.error("网点已处理任务队列出错", e);
            throw new SinoException(CenterResponseEnum.NET_HANDLE_LIST_ERROR);
        }
        log.info("查询网点已处理任务队列结束");
        return dto;
    }

    private Dto getTaskList(NetHandle netHandle) {
        Integer pageIndex = netHandle.getPage();//页数
        Integer pageSize = netHandle.getPageSize();//条数
        String tellerNo = netHandle.getTellerNo();
        String start = netHandle.getOperateFrom();
        String end = netHandle.getOperateTo();
        String printNum = netHandle.getPrintNum();
        String kernelNo = netHandle.getKernelNo();
        String journalNo = netHandle.getJournalNo();
        String transState = netHandle.getTransState();
        String orgCode = netHandle.getOrgCode();
        int count = transListService.getTransListByNetHandleCount(tellerNo, start, end, printNum, kernelNo, journalNo, transState, orgCode);
        Dto dto = new Dto();
        if (count > 0) {
            List<NetHandleEntity> allNetHandleEntityQueues = transListService.getTransListByNetHandleList(tellerNo, start, end, printNum, kernelNo, journalNo, transState, orgCode);
            List<NetHandleEntity> taskQueues = PaginateUtil.execute(allNetHandleEntityQueues, pageIndex, pageSize, count);
            dto.setCount(count);
            dto.setData(taskQueues);
        }
        return dto;
    }
}
